<template>
  <div class="app-container">
    <el-form :inline="true" :model="formInline" label-suffix="：" class="demo-form-inline" label-width="100px" label-position="right">
      <el-form-item label="用户名称">
        <el-input v-model="formInline.user_name" placeholder="商品名称" />
      </el-form-item>
      <el-form-item label="角色">
        <el-select v-model="formInline.roles" placeholder="请选择角色">
          <el-option v-for="item in role_list" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="getList">查询</el-button>
        <el-button size="small" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="tableData"
      border
      fit
      :header-cell-style="{
        textAlign: 'center',
        color: '#909399',
        backgroundColor: '#f8f8f8',
      }"
    >
      <el-table-column label="ID" prop="id" align="center" />
      <el-table-column label="头像" prop="DiscountAmount" align="center">
        <template #default="{ row }">
          <div>
            <el-image
              :src="row.avatar ? $baseUrl + row.avatar : 'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png'"
              style="width: 100px; height: 100px"
              :preview-src-list="[
                row.avatar ? $baseUrl + row.avatar : 'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png',
              ]"
            ></el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="用户名" prop="user_name" align="center" />
      <el-table-column label="密码" prop="password" align="center">
        <template #default="{ row, $index }">
          <div>
            <span v-if="tableData[$index].showPwd">
              {{ row.password }}
              <vab-icon :icon="['fas', 'eye-slash']" style="padding-left: 5px" @click="hiddePwd($index)"></vab-icon>
            </span>
            <span v-else>
              ********
              <vab-icon :icon="['fas', 'eye']" style="padding-left: 5px; vertical-align: top" @click="openPwd($index)"></vab-icon>
            </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="手机号" prop="phone" align="center" />
      <el-table-column label="角色" prop="introduction" align="center" />
      <el-table-column label="审核状态" prop="status" align="center">
        <template #default="{ row }">
          <el-tag :type="row.status == 2 ? 'success' : row.status == 3 ? 'danger' : ''">
            {{ row.status === 1 ? '待审核' : row.status === 2 ? '审核通过' : '审核拒绝' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="账号状态" prop="isDisabled" align="center">
        <template #default="{ row }">
          <el-tag :type="row.isDisabled == 2 ? 'success' : 'danger'" v-if="row.isDisabled">
            {{ row.isDisabled === 1 ? '已禁用' : '已启用' }}
          </el-tag>
          <el-tag type="danger" v-else>未通过审核</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" prop="create_time" align="center" />
      <el-table-column label="操作" prop="handle" align="center" fixed="right" width="200">
        <template #default="{ row }">
          <el-col>
            <el-row :span="24" style="margin-bottom: 10px">
              <el-button
                :type="row.status != 1 ? 'info' : 'primary'"
                size="small"
                icon="el-icon-lock"
                @click="Audit(row)"
                class="btn_class"
                :disabled="row.status != 1"
              >
                审核
              </el-button>
              <el-button
                :type="row.status != 2 ? 'info' : 'primary'"
                size="small"
                icon="el-icon-edit"
                @click="OpenHandle(row)"
                class="btn_class"
                :disabled="row.status != 2"
              >
                编辑
              </el-button>
            </el-row>
            <el-row :span="24">
              <el-button
                size="small"
                :type="row.status != 2 ? 'info' : 'primary'"
                :disabled="row.status != 2"
                icon="el-icon-switch-button"
                class="btn_class"
                v-if="row.isDisabled == 1"
                @click="disableUser(row.id, 2)"
              >
                启用
              </el-button>
              <el-button
                size="small"
                :type="row.status != 2 ? 'info' : 'danger'"
                :disabled="row.status != 2"
                icon="el-icon-switch-button"
                class="btn_class"
                v-else
                @click="disableUser(row.id, 1)"
              >
                禁用
              </el-button>
              <el-button
                size="small"
                :type="row.status != 2 ? 'info' : 'danger'"
                :disabled="row.status != 2"
                icon="el-icon-delete"
                @click="deleteUser(row.id)"
                class="btn_class"
              >
                删除
              </el-button>
            </el-row>
          </el-col>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="formInline.total > 0"
      :total="formInline.total"
      :page.sync="formInline.page"
      :limit.sync="formInline.limit"
      @pagination="getList"
    />
    <handle ref="handle" @getList="getList"></handle>
    <audit ref="audit" @getList="getList"></audit>
  </div>
</template>

<script>
  import handle from './components/handle.vue'
  import audit from './components/audit.vue'
  import Pagination from '@/components/Pagination'
  import { getUserList, disabledUesr, deleteUesr } from '@/api/user'
  export default {
    components: { Pagination, handle, audit },
    data() {
      return {
        listLoading: false,
        formInline: {
          total: 10,
          page: 1,
          limit: 10,
        },
        role_list: [
          { value: 'admin', label: '超级管理员' },
          { value: 'user', label: '普通商家' },
        ],
        showPwd: false,
        tableData: [],
      }
    },
    mounted() {
      this.getList()
    },
    methods: {
      async getList() {
        this.listLoading = true
        await getUserList(this.formInline).then(({ data }) => {
          console.log('data: ', data)
          data.list.forEach((item) => {
            item.showPwd = false
          })
          this.tableData = data.list
          this.formInline.total = data.total
        })
        this.listLoading = false
      },
      disableUser(id, status) {
        console.log('id, status: ', id, status)
        this.$confirm(`是否确认${status == 1 ? '禁用' : '启用'}该用户?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            await disabledUesr({ id, isDisabled: status }).then((data) => {
              if (data.code == 200) {
                this.$message({
                  type: 'success',
                  message: `${status == 1 ? '禁用' : '启用'}成功`,
                })
                this.getList()
              } else {
                this.$message({
                  type: 'error',
                  message: data.msg,
                })
              }
            })
          })
          .catch(() => {})
      },
      Audit(row) {
        this.$refs.audit.open(row)
      },
      openPwd(idx) {
        this.tableData[idx].showPwd = true
        console.log('this.tableData: ', this.tableData)
      },
      hiddePwd(idx) {
        this.tableData[idx].showPwd = false
        console.log('this.tableData: ', this.tableData)
      },
      reset() {
        this.formInline = {
          total: 10,
          page: 1,
          limit: 10,
        }
        this.getList()
      },
      OpenHandle(row) {
        this.$refs.handle.open(row)
      },
      deleteUser(id) {
        this.$confirm('是否确认删除该用户？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            await deleteUesr({ id }).then((data) => {
              if (data.code == 200) {
                this.$message({
                  type: 'success',
                  message: '删除成功!',
                })
                this.getList()
              } else {
                this.$message({
                  type: 'error',
                  message: data.msg,
                })
              }
            })
          })
          .catch(() => {})
      },
    },
  }
</script>

<style scoped lang="scss">
  .btn_class {
    width: 80px !important;
  }
</style>
